package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal){
        log.info("新增套餐，参数：{}",setmeal);
        setmealService.add(checkgroupIds,setmeal);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds, @RequestBody Setmeal setmeal){
        log.info("编辑套餐，参数：{}",setmeal);
        setmealService.edit(checkGroupIds,setmeal);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 根据id查找套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询套餐，参数：{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐，参数：{}",queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        log.info("上传图片，参数：{}",imgFile);
        try {
            if (imgFile == null) {
                log.error("上传的文件为空");
                return new Result(false, "未能上传图片", null);
            }
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名的后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成新的文件名
            String newFileName = UUID.randomUUID().toString() + suffix;

            //文件的请求路径
            String filePath = aliOssUtil.upload(imgFile.getBytes(), newFileName);
            // 将图片路径添加到Redis的uploadedImages集合
            redisTemplate.opsForSet().add("uploadedImages", filePath);


            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error(MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    @Autowired
    private void setAliOssUtil(AliOssUtil aliOssUtil){
        this.aliOssUtil = aliOssUtil;
    }
}
